﻿using IMModel.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using IMModel.Domain.Common;
using System.Data.Linq;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using IMModel;

namespace IMTests
{


    /// <summary>
    ///This is a test class for TermTest and is intended
    ///to contain all TermTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TestGet
    {
        /// <summary>
        /// список респондентов, градаций
        ///</summary>
        //[TestMethod()]
        public void GetIndexesString()
        {
            using (var ctx = new IMDataDataContext("Data Source=ugtudb;Initial Catalog=IndexMark;Integrated Security=True"))
            {
                var term = ctx.Terms.Single(x => x.IDTerm == 8);
                /// список респондентов
                //Console.WriteLine(string.Join(",  ", term.Respondents.Select(x => x.Person.ShortName + " - " + x.FlagOsnJob)));
                /// список градаций
                //Console.WriteLine(string.Join(",  ", term.GradationTerms.Select(x => x.Gradation.Name + " - " + x.GradationsAnswers.Count + " шт.")));
                /// список показателей
                Console.WriteLine(
                    string.Join("|||||   ", term.CategoryTerms.Select(x =>
                    string.Join(";;;   ", x.CategorySectionTerms.Select(y =>
                    string.Join(",   ", y.CharacteristicTerms.Select(z =>
                        z.Characteristic.Name + " = " + z.Weight
                    )))))));
            }
        }

        string GetAnswer(Answer a)
        {
            return a.Respondent.Person.ShortName + " - " + a.Respondent.Department.Name;
        }

        /// <summary>
        /// получение списка истории изменений
        ///</summary>
        //[TestMethod()]
        public void GetHistoryAnswers()
        {
            using (var ctx = new IMDataDataContext("Data Source=ugtudb;Initial Catalog=IndexMark;Integrated Security=True"))
            {
                var term = ctx.Terms.Single(x => x.IDTerm == 8);
                foreach (var item in term.Respondents)
                {
                    Console.Write(string.Join(",  ", item.Answers.Select(x => x.Answer1 == null ? "-" : GetAnswer(x.Answer1))));
                    Console.Write("; ");
                }
            }
        }
        /// <summary>
        /// получение списка изменений
        ///</summary>
        //[TestMethod()]
        public void GetAllAnswers()
        {
            using (var ctx = new IMDataDataContext("Data Source=ugtudb;Initial Catalog=IndexMark;Integrated Security=True"))
            {
                var term = ctx.Terms.Single(x => x.IDTerm == 8);
                foreach (var item in term.Respondents)
                {
                    //Console.WriteLine(string.Join(",  ", item.Answers.Select(x => GetAnswer(x))));
                    Console.WriteLine(item.Person.ShortName + " - " + item.Department.Name);
                }
            }
        }

        string Answ(Answer a)
        {
            var c = a.CharacteristicTerm;
            return string.Format("{2}: Vo={0}, Va={1}, (вес={3})", a.Volume, a.Value,
                c.Characteristic.Name, c.Weight);
        }

        /// <summary>
        /// поиск градаций с весом = 0
        ///</summary>
        //[TestMethod()]
        public void GetAnswers()
        {
            using (var ctx = new IMDataDataContext("Data Source=ugtudb;Initial Catalog=IndexMark;Integrated Security=True"))
            {
                var term = ctx.Terms.Single(x => x.IDTerm == 6);
                var context = term.GetIndexContextDefault();

                //Console.WriteLine(string.Join("    |    ", term.GradationTerms.Where(x => x.Weight == 0)
                //    .Select(x => GetTree(x) + " - " + x.Weight)));

                // поиск Volume == 0
                Console.WriteLine(string.Join("    |    ", term.Respondents.Select(x =>
                   x.Person.ShortName + ": " + string.Join("; ", x.Answers.Where(p =>
                       //p.CharacteristicTerm.Weight == 0
                    p.Volume == 0).Select(a => Answ(a))))));

                // очистка поиск градаций с весом = 0
                //foreach (var item in term.GradationTerms)
                //{
                //    if (item.Weight == 0) item.Weight = null;
                //}
                //ctx.SubmitChanges();
            }
        }

        [TestMethod()]
        public void GetPremiums()
        {
            using (var ctx = new IMDataDataContext(
                "Data Source=ugtudb;Initial Catalog=IndexMark;Integrated Security=True"))
            {
                var term = ctx.Terms.Single(x => x.IDTerm == 8);
                var c = term.GetIndexContextPPS();
                var Respondents = term.PremiumParticipants(c);

                //List<DTO.Money> list = new List<DTO.Money>();
                foreach (Respondent Resp in Respondents)
                {
                    Console.WriteLine(Resp + ";" + Resp.Premium);
                }
            }
        }
    }
}